#!/usr/bin/env python
# coding=utf-8
'''
Created on 2010-7-31

@author: Jason.q.yan
'''
import os, sys, re, traceback, logging
import urllib
import time
import datetime
import getopt
import math
import urllib2
from Route import *

routeDiv_re = re.compile(r'''
		<td\swidth="485"\svalign="top"><table\swidth="455"\sborder="0"\salign="center"\scellpadding="0"\scellspacing="0"\sclass="table_05">([\s\S]+?)\s+</a></td>
        ''', re.X|re.S)
 
schedule_re =  re.compile(r'''
        出行攻略([\s\S]+?)可选择酒店列表
        ''', re.X|re.S)
server_urls = [('自由行','http://www.cnto.com/freetrip/freetrip_reserve_info.asp?page=%d',)]
def do_crawl():
    try:
        for vacation_type, server_url in server_urls:
            #获取总线路数算出总页数-此网站占时无分页
            pageTotal = 1
            #根据总页数，遍历所有线路列表页
            for pageNo in range(1,pageTotal+1):
                #获取所有描述线路的HTML片段 
                html = urllib2.urlopen(server_url % pageNo).read().decode('gb2312','ignore').encode('utf-8')
                route_list = routeDiv_re.findall(html)
                #遍历，解析
                for div in route_list:
                    route = Route()
                    route.site = 33
                    route.supplier_url = 'http://www.cnto.com/'
                    route.supplier = "广之旅"
                    route.outcity = '广州'
                    route.go_t = route.back_t = "-"
                    all = re.compile(r'''<a\shref="([\s\S]+)"\s>([\s\S]+)</a>&nbsp;&nbsp;''', re.X|re.S).search(div)
                    if all:
                        route.title = all.group(2)
                        route.rout_url = route.supplier_url+"freetrip/"+all.group(1)
                        date_result = re.compile(r'''<td\svalign="top"\sclass="td_13">出发班期</td>\s+<td>([\s\S]+?)</td>''', re.X|re.S).search(div)
                        if ( str(date_result.group(1)).count('t')==0):
                            route.dates = date_result.group(1)
                            if (str(route.dates).count('月') & str(route.dates).count('日') ):
                                route.dates =route.dates.replace("-", ";")
                                route.dates ="2010-"+route.dates.replace("月", "-")
                                route.dates =route.dates.replace("日", "")
                                route.dates =route.dates.replace(";", ";2010-")
                                route.dates=route.dates+";"
                        route.telphone="400-863-8888"
                        route.type = "自由行"
                        route.img_url= re.compile(r'''<img\ssrc=([\s\S]+?)\sname="v_path"\s+?width=128\sborder=0\sclass="imgborder">''', re.X|re.S).search(div).group(1)
                        if str(route.img_url).count(':')==0:
                            route.img_url =route.img_url.replace("\"", "")
                            route.img_url ="http://www.cnto.com"+route.img_url
                        route.price = re.compile(r''' <span\sclass="price">(\d+)元</span>''', re.X|re.S).search(div).group(1)
                        route.days = "1"
                        #根据URL获取线路详细页面的HTML
                        temp = urllib2.urlopen(route.rout_url).read().decode('gb2312','ignore').encode('utf-8')
                        #进一步解析 
                        do_parse(temp,route)
                        print >>sys.stderr, 'updating', route.supplier, route.title
                        route.updRoute()
                        route.updRoute_Schedules()
                
    except:
        print traceback.format_exc(sys.exc_info())

#解析每一天的行程安排
def do_parse(html,route):
    detail = re.compile(r'''出发班期([\s\S]+?)<br\s/>\s+<br\s/>\s+([\s\S]+?)</td>''', re.X|re.S).search(html).group(2)
    if str(detail).count("</table>")>0 :
        route.detail=""
    else:
        route.detail=detail
    schedule_list = schedule_re.findall(html)
    for parthtml in schedule_list:
        if re.compile(r'''第(\S+)天''', re.X|re.S).search(parthtml):
            days_list=re.compile(r'''第(\S+)天''', re.X|re.S).findall(parthtml)
            if len(days_list)>0:
                route.days = str(len(days_list))
    return 

if __name__ == '__main__':
    script_path = os.path.dirname(os.path.realpath(__file__))
    os.chdir(script_path)
    do_debug = False

    try:
        opts, args = getopt.gnu_getopt(sys.argv[1:], 'hdno:p:', ['help', 'debug', 'no-cache', 'only-cache', 'provider'])
    except getopt.GetoptError:
        usage()
        sys.exit()

    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
            sys.exit()
        elif opt in ('-d', '--debug'):
            do_debug = True
        elif opt in ('-n', '--no-cache'):
            use_cache = False
        elif opt in ('-o', '--only-cache'):
            if arg.lower() in ('no', 'n', '0'):
                only_cache = False
            else:
                only_cache = True
        elif opt in ('-p', '--provider'):
            pass

    if do_debug:
        import pdb
        pdb.set_trace()
    do_crawl()
